package com.example.yypt.utils;

import com.example.yypt.model.xml.Message;
import com.example.yypt.model.xml.Response;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XmlUtilsHelp {
	private static XStream xstream;

	static {
		xstream = new XStream(new DomDriver());
		xstream.alias("response", Response.class);
		xstream.alias("message", Message.class);
		xstream.addImplicitCollection(Response.class, "messageList");
	}

	// xml转java对象
	public static Object xmlToBean(String xml) {
		return xstream.fromXML(xml);
	}

	// java对象转xml
	public static String beanToXML(Object obj) {
		return "<?xml version=\"1.0\" encoding=\"GB2312\"?>"
				+ xstream.toXML(obj);
	}

	// java对象转xml
	public static String beanToXML2(Object obj) {
		return "<?xml version=\"1.0\" encoding=\"GB2312\"?>"
				+ xstream.toXML(obj);
	}

}
